﻿using System;
using System.Configuration;
using Framework.Email.Configuration;
using Framework.Exceptions;
using Interfaces.Infrastructure;

namespace Framework.Email
{
    public class EmailService : IEmailService
    {
        private const string DefaultEmailConfigurationSectionName = "emailService";

        public void Send(string toEmail, object templateModel, string templateKey = null, string configurationSectionName = DefaultEmailConfigurationSectionName)
        {
            if (toEmail == null)
                throw new ArgumentNullException("toEmail");

            if (templateModel == null) 
                throw new ArgumentNullException("templateModel");
            
            try
            {
                var configuration = ConfigurationManager.GetSection(configurationSectionName ?? DefaultEmailConfigurationSectionName)
                               as EmailServiceConfigurationSection;

                if (configuration == null)
                    throw new ConfigurationErrorsException("Invalid email templates configuration section");

                var key = templateKey ?? templateModel.GetType().Name;
                var template = configuration.EmailTemplateNames[key];


                if (!string.IsNullOrEmpty(template.Template))
                {
                    FluentEmail.Email
                                       .From(configuration.FromEmail)
                                       .To(toEmail)
                                       .Subject(template.Subject)
                                       .UsingTemplate(template.Template, templateModel, false)
                                       .Send();
                }
                else
                {
                    if (!string.IsNullOrEmpty(template.TemplateUrl))
                    {
                        FluentEmail.Email
                                      .From(configuration.FromEmail)
                                      .To(toEmail)
                                      .Subject(template.Subject)
                                      .UsingTemplateFromFile(template.Template, templateModel, false)
                                      .Send();
                    }
                    else
                        throw new ConfigurationErrorsException("Invalid template or email message");
                }
            }
            catch (Exception exception)
            {
                if(ExceptionHandlingManager.HandleException(exception,PolicyNames.EmailServicePolicy))
                     throw;
            }
        }

        public void Send(string toEmail ,string message, object templateModel,string subject, string fromEmail)
        {
            if(toEmail == null)
                throw new ArgumentNullException("toEmail");

            if(fromEmail == null)
                throw new ArgumentNullException("fromEmail");

            if(templateModel == null)
                throw new ArgumentNullException("templateModel");
            
            try
            {
                FluentEmail.Email
                                   .From(fromEmail)
                                   .To(toEmail)
                                   .Subject(subject)
                                   .UsingTemplate(message, templateModel, false)
                                   .Send();
            }
            catch (Exception exception)
            {
                if (ExceptionHandlingManager.HandleException(exception, PolicyNames.EmailServicePolicy))
                    throw;
            }
        }
        

    }
}
